local GameDB = import(".GameDB")

function GameDB:queryAllFameSubTypes()
    return self:getCachedValues("fame_subkey")
end

function GameDB:queryAllFameSubKeys()
	return self:getCachedTable("fame_subkey")
end

function GameDB:getFameSubKeyById(id)
    local config = self:getCachedValue("fame_subkey",id)
    return config
end

function GameDB:getAllFames()
    local fameConfig = self:getCachedTable("fame")
    return fameConfig
end

function GameDB:queryFameLevelUpById(fameLevelId, flag)
    local config
    if flag then
        config = self:getCachedValue("fameWorld_levelup",fameLevelId)
    else
        config = self:getCachedValue("fame_levelup",fameLevelId)
        
    end
    return config
end

function GameDB:queryFameById(fameId)
    local config = self:getCachedValue("fame",fameId)
    return config
end

-- 缓存 排好序的fame升级配置
GameDB._fameLvUpConfs = nil
function GameDB:queryFameLevelUp(flag)
    if flag then
        self._fameLvUpConfs =  self:getCachedValues("fameWorld_levelup", nil, function(l, r)
			return tonumber(l.ID) < tonumber(r.ID)
        end)
    else
		self._fameLvUpConfs =  self:getCachedValues("fame_levelup", nil, function(l, r)
			return tonumber(l.ID) < tonumber(r.ID)
		end)
    end
	return self._fameLvUpConfs
end

function GameDB:queryFameBySubKey(subKey)
    return self:getCachedValues("fame",function(v)
        return tonumber(v.subkey) == tonumber(subKey)
    end)
end

function GameDB:formatFameLevelIconFilePath(iconName)
	local iconPath = nil
	local suffix = "png"
	local path = Res.fameLevelPath

	if iconName then
		iconPath = string.format("%s%s.%s",path,iconName,suffix)
	end

	return iconPath
end

-- 获取声望列表
function GameDB:getFameLists()
	local list = {}
	local dict = {}
	local allFames = self:getAllFames()
	for i, v in pairs(allFames) do
		local temp = {}
		temp.id = tonumber(v.ID)
		temp.name = L(v.name)
		temp.des = tonumber(v.des)
		temp.npc = tonumber(v.npc) -- 追踪用NPC
		temp.quest = tonumber(v.quest) -- 追踪开启任务
		-- temp.icon = tonumber(v.icon) -- 底图、二级菜单好像没有底图。应该移动到1级菜单上
		temp.sort = tonumber(v.sort) -- 排序用
		temp.subkey = tonumber(v.subkey) -- 所属大类ID
		temp.maxExp = tonumber(v.maxExp) -- 改声望能达到的最大值
        temp.featureon = tonumber(v.featureon)

		temp.level = 0 -- 具体声望等级
		temp.exp = 0 -- 具体声望值

        local config = self:getFameSubKeyById(temp.subkey) -- 一级菜单信息
        if not config then 
            print("GameDB_fame:getFameList()=========== fame_subkey is not find")
            return 
        end
        local isHaveSubMenuFlag = tonumber(config.showType) ~= 1 -- 是否有二级菜单
        local isOpen = UIHandler:isFunctionOpen(temp.featureon)
        if isOpen then 
            if not dict[temp.subkey] then
                local temp2 = {}
                temp2.id = tonumber(config.ID)
		        temp2.name = L(config.name)
		        temp2.sumFame = 0 -- 声望总和
		        temp2.curFame = 0 -- 当前声望		    
		        temp2.bgName = config.banner -- 底图
		        temp2.subDatas = {}
                temp2.showType = tonumber(config.showType) --是否有二级菜单（1表示没有）

                list[#list+1] = temp2
		        dict[temp2.id] = temp2 
            end 
            if isHaveSubMenuFlag then
                local datas = dict[temp.subkey].subDatas
		        datas[#datas + 1] = temp
            end 
        end
	end
	local function sort(a,b)
		return a.id < b.id
	end
	local function subSort(a,b)
		return a.sort < b.sort
	end
	table.sort(list, sort)
	for i,v in ipairs(list) do
		table.sort(v.subDatas, subSort)
	end
	return list
end

-- 声望全部升满需要的总经验值
GameDB._fameFullExp = nil
function GameDB:getFameFullExp(flag)
	local exp = 0
	local confs = self:queryFameLevelUp(flag)
	for _, conf in ipairs(confs) do
		exp = exp + tonumber(conf.exp)
	end
	self._fameFullExp = exp
	return self._fameFullExp
end

function GameDB:getFameTotalCount(subKey)
    local isWorldFameFlag = self:isWorldFame(subKey)
	local iEachCount = self:getFameFullExp(isWorldFameFlag)

	local fames = self:queryFameBySubKey(subKey)
	return #fames * iEachCount
end 

function GameDB:getFameNameLevelExp(fameId, flag)
    local level = 0
    local exp = 0
    local totalExp = 0
    local levelName = ""
    local fame = UD:getFameWithId(fameId)
    if fame then
        level = fame.level
        exp = fame.exp
    end
    local fameLevelC = self:queryFameLevelUpById(level, flag)
    levelName = L(fameLevelC and fameLevelC.name or "")
    totalExp = fameLevelC and fameLevelC.exp or totalExp

    local fameC = self:queryFameById(fameId)
    local fameName = (fameC and L(fameC.name) or fameId.." not found")

    return level,exp,totalExp,levelName,fameName
end

function GameDB:queryWorldFameLevelUpById(fameLevelId)
    local config = self:getCachedValue("fameWorld_levelup",fameLevelId)
    return config
end

function GameDB:isWorldFame(subKey)
     local config = self:getFameSubKeyById(subKey)
     if config and tonumber(config.showType) == 1 then
        return true
     end
     return false
end

function GameDB:checkFameIsOpenBySubKey(subKey)
    local flag = false
    local fames = self:queryFameBySubKey(subKey)
    for i, v in ipairs(fames) do
        local isOpen = UIHandler:isFunctionOpen(tonumber(v.featureon))
        if isOpen then
            flag = true
            break
        end
    end
    return flag
end

return GameDB
